/*
 * Copyright © 2020-present LiuDanYang. All rights Reserved.
 */

/**
 * 获取URL信息
 * @returns {{route: string, options: *, url: string}}
 */
export function getUrl() {
  // eslint-disable-next-line no-undef
  const pages = getCurrentPages(); // 获取加载的页面
  const currentPage = pages[pages.length - 1]; // 获取当前页面的对象

  const route = currentPage.route; // 当前页面url
  const options = currentPage.options; // 如果要获取url中所带的参数可以查看options

  // 参数多时通过&拼接url的参数
  let urlWithArgs = route + "?";
  for (const key in options) {
    const value = options[key];
    urlWithArgs += key + "=" + value + "&";
  }
  urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1);
  return {
    route: route,
    options: options,
    url: "/" + urlWithArgs
  };
}

/**
 * URL加码
 * @param url
 * @returns {string}
 */
export function urlEnCode(url) {
  url = (url + "").toString();
  return encodeURIComponent(url)
    .replace(/!/g, "%21")
    .replace(/'/g, "%27")
    .replace(/\(/g, "%28")
    .replace(/\)/g, "%29")
    .replace(/\*/g, "%2A")
    .replace(/%20/g, "+");
}

/**
 * URL解码
 * @param url
 * @returns {string}
 */
export function urlDeCode(url) {
  return decodeURIComponent(url);
}
